package com.chezhujie.www.utils;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DESUtils {

    private byte[] desKey;

    public DESUtils(String desKey) {
        this.desKey = desKey.getBytes();
    }

    public byte[] desEncrypt(byte[] plainText) throws Exception {
        SecureRandom sr = new SecureRandom();
        byte rawKeyData[] = desKey;
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key, sr);
        byte data[] = plainText;
        byte encryptedData[] = cipher.doFinal(data);
        return encryptedData;
    }

    public byte[] desDecrypt(byte[] encryptText) throws Exception {
        SecureRandom sr = new SecureRandom();
        byte rawKeyData[] = desKey;
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key, sr);
        byte encryptedData[] = encryptText;
        byte decryptedData[] = cipher.doFinal(encryptedData);
        return decryptedData;
    }

    public String encrypt(String input) throws Exception {
        String temp = base64Encode(desEncrypt(input.getBytes()));
        return replaceBlank(temp);
    }

    public String decrypt(String input) throws Exception {
        byte[] result = base64Decode(input);
        return new String(desDecrypt(result));
    }

    public static String base64Encode(byte[] s) {
        if (s == null)
            return null;
        BASE64Encoder b = new sun.misc.BASE64Encoder();
        return b.encode(s);
    }

    public static byte[] base64Decode(String s) throws IOException {
        if (s == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = decoder.decodeBuffer(s);
        return b;
    }
    
    private String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    public static void main(String[] args) throws Exception {
       /* String key = "cvO49vTHR33fyg";
        String input = "{\"provider\":\"chinaunicom\",\"moblieNo\":\"18502087329\",\"cityCode\":\"440100\"}";
        
        
        System.out.println(input);
        DESUtils crypt = new DESUtils(key);
        String cryptStr = crypt.encrypt(input);
        System.out.println(cryptStr);
        String result = crypt.decrypt(cryptStr);	
        System.out.println(result);
        
        
        String appkey = "kiTekj114kgSi12TkTTkQtQ8924951Rs";
        String str = crypt.encrypt(appkey);
        System.out.println(str);
        String str1 = crypt.decrypt(str);
        System.out.println(str1);*/
        
        String key2 = "kiTekj114kgSi12TkTTkQtQ8924951Rs";
        String input2 = "{\"unique_id\":\"13539925170\",\"type\":\"mobile\"}";
        DESUtils crypt2 = new DESUtils(key2);
        String cryptStr2 = crypt2.encrypt(input2);
        System.out.println(cryptStr2);
        		
        
    	//System.out.println(89999999*Math.random()+10000000+"---"+(int)(Math.random()*100));
//    	 Integer code;
//         while(true){
//    		code=(int)(Math.random()*1000000);
//    		System.out.println(code);
//    		if(code>99999){
//    			break;
//    		}  		
//         }
        // System.out.println(code);
//    	for (int i = 0; i <10; i++) {
//    		int index=(int)(Math.random()*100);
//    		System.out.println(index);
//		}
    }
}